//
// Created by ins on 2021/2/27.
//

#include <kisql.h>
#include <tool/Util.h>
#include "PlayerDao.h"
PlayerDao *PlayerDao::sInstance = new PlayerDao();
static kisql *db;
static MYSQL_RES *result;
static MYSQL_ROW row;

PlayerDao::PlayerDao() {
    db = kisql::getInstance();
}
PlayerDao::~PlayerDao() {}
PlayerDao* PlayerDao::getInstance() {
    return sInstance;
}

void PlayerDao::addPlayer(PlayerBean playerBean) {}
void PlayerDao::delPlayer(PlayerBean playerBean) {}
void PlayerDao::updatePlayer(PlayerBean playerBean) {}
void PlayerDao::getPlayer(string sid, PlayerBean &retPlayer) {
    char sql[1024] = {0};
    sprintf(sql, "select * from game where sid = \"%s\"", sid.c_str());

    if(mysql_query(db->getMysql(), sql)){
        perror("mysql query err");
        return;
    } else{ //select success
        result = mysql_store_result(db->getMysql());
        if(result){
            int num_rows = mysql_num_rows(result);

            for(int i = 0; i < num_rows; i++){
                row = mysql_fetch_row(result);
                if(row == nullptr){
                 break;
                }

                retPlayer.setUid(row[0]);  // uid
                retPlayer.setSid(row[1]);  // sid
                //retPlayer.setSid(row[2]);  // token
                retPlayer.setName(row[3]);  // name
                retPlayer.setLv(row[4]);  // lv
                retPlayer.setYxb(row[5]);  // yxb
                retPlayer.setExp(row[6]);  // exp
                retPlayer.setGreder(row[7]);
                retPlayer.setHp(row[8]);  //
                retPlayer.setMaxHp(row[9]);
                retPlayer.setAttack(row[10]);
                retPlayer.setDefence(row[11]);
                retPlayer.setBaoji(row[12]);
                retPlayer.setWuxing(row[13]);
                retPlayer.setXixue(row[14]);
                retPlayer.setCw(row[15]);
                retPlayer.setNowMid(row[16]);
                retPlayer.setTool1(row[18]);
                retPlayer.setTool2(row[19]);
                retPlayer.setTool3(row[20]);
                retPlayer.setTool4(row[21]);
                retPlayer.setTool5(row[22]);
                retPlayer.setTool6(row[23]);
                retPlayer.setYp1(row[24]);
                retPlayer.setYp2(row[25]);
                retPlayer.setYp3(row[26]);
                retPlayer.setJn1(row[27]);
                retPlayer.setJn2(row[28]);
                retPlayer.setJn3(row[29]);
                retPlayer.setVip(row[30]);
                retPlayer.setIsOnline(row[31]);
                retPlayer.setSfxl(row[32]);
                retPlayer.setIspvp(row[33]);
                retPlayer.setEndTime(row[34]);
                retPlayer.setCzb(row[36]);
                retPlayer.setMaxExp(row[37]);

                cout << "player:" << retPlayer.toString() << endl;

            }
     }
    }
}

bool PlayerDao::changeExp(string sid, string exp) {
    string sql = "update game set uexp = uexp +" + exp + "where sid = " + sid;
    if(mysql_query(db->getMysql(), sql.c_str())){
        perror("mysql query err");
        return false;
    }else{ //select success
        //update insert delete
        if(mysql_field_count(db->getMysql()) == 0){
            return true;
        }
    }
    return false;
}

/**
 * update player last online time
 * @param sid player`s sid
 * @return exe success or failed
 */
bool PlayerDao::updatePlayerEndTime(string sid) {
    char sql[1024] = {0};

    sprintf(sql, R"(update game set endtime = "%s" , sfzx = 1 where sid = "%s")",
            Util::getTime().c_str(), sid.c_str());

    if(mysql_query(db->getMysql(), sql)){
        return false;
    }else{ //select success
        if(mysql_field_count(db->getMysql()) == 0){
            return true;
        }
    }
    return false;
}
/**
 * update player`s some mibName with mibValue
 * @param sid player`s sid
 * @param mibName
 * @param mibValue
 * @return exe success or failed
 */
bool PlayerDao::updatePlayerSX(string sid, string mibName, string mibValue) {
    char sql[1024] = {0};
    sprintf(sql, "update game set %s = \"%s\" WHERE sid = \"%s\"",
             mibName.c_str(), mibValue.c_str(), sid.c_str());
    cout << "sql: " << sql << endl;

    if(mysql_query(db->getMysql(), sql)){
        return false;
    }else{ //select success
        if(mysql_field_count(db->getMysql()) == 0){
            return true;
        }
    }
    return false;
}

void PlayerDao::getAllPlayer(string nowmid, list<PlayerBean> &allplayers) {
    string sql = "select * from game where sfzx = 1 and nowmid = "+ nowmid;
    PlayerBean player;
    if(mysql_query(db->getMysql(), sql.c_str())){
        perror("mysql query err");
        return;
    } else{ //select success
        result = mysql_store_result(db->getMysql());
        if(result){
            int num_rows = mysql_num_rows(result);
            //int num_fields = mysql_num_fields(result);

            for(int i = 0; i < num_rows; i++){
                row = mysql_fetch_row(result);
                if(row == nullptr){
                    break;
                }

                player.setUid(row[0]);  // uid
                player.setSid(row[1]);  // sid
                //retPlayer.setSid(row[2]);  // token
                player.setName(row[3]);  // name
                player.setLv(row[4]);  // lv
                player.setYxb(row[5]);  // yxb
                player.setExp(row[6]);  // exp
                player.setGreder(row[7]);
                player.setHp(row[8]);  //
                player.setMaxHp(row[9]);
                player.setAttack(row[10]);
                player.setDefence(row[11]);
                player.setBaoji(row[12]);
                player.setWuxing(row[13]);
                player.setXixue(row[14]);
                player.setCw(row[15]);
                player.setNowMid(row[16]);
                player.setTool1(row[18]);
                player.setTool2(row[19]);
                player.setTool3(row[20]);
                player.setTool4(row[21]);
                player.setTool5(row[22]);
                player.setTool6(row[23]);
                player.setYp1(row[24]);
                player.setYp2(row[25]);
                player.setYp3(row[26]);
                player.setJn1(row[27]);
                player.setJn2(row[28]);
                player.setJn3(row[29]);
                player.setVip(row[30]);
                player.setIsOnline(row[31]);
                player.setSfxl(row[32]);
                player.setIspvp(row[33]);
                player.setEndTime(row[34]);
                player.setCzb(row[36]);
                player.setMaxExp(row[37]);
                allplayers.push_back(player);
                cout << "player:" << player.toString() << endl;

            }
        }
    }
}
